﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc.Html;

namespace QuickApp.MVCControl.FlexiGrid
{
    /// <summary>
    /// 表格的按钮信息集合
    /// </summary>
    /// <typeparam name="T">对象类型</typeparam>
    public class FlexiGridButtonCollection<T> : ICollection<FlexiGridButton> where T : class
    {
        #region Private fields
        /// <summary>
        /// 列信息集合
        /// </summary>
        private readonly IList<FlexiGridButton> _buttons = new List<FlexiGridButton>();
        #endregion

        #region Add

        /// <summary>
        /// 添加一个按钮
        /// </summary>
        /// <param name="name">按钮名称</param>
        /// <param name="title">按钮显示标题</param>
        /// <returns></returns>
        public FlexiGridButton Bind(string name, string title, FlexiGridButtonType type, string onPressFunction, string toolTip = null)
        {
            return Bind(name, title).Type(type).OnPress(onPressFunction).ToolTip(toolTip ?? title);
        }

        /// <summary>
        /// 添加一个按钮
        /// </summary>
        /// <param name="name">按钮名称</param>
        /// <param name="title">按钮显示标题</param>
        /// <returns></returns>
        public FlexiGridButton Bind(string name, string title, FlexiGridButtonType type)
        {
            return Bind(name, title).Type(type);
        }

        /// <summary>
        /// 添加一个按钮
        /// </summary>
        /// <param name="title">按钮显示标题</param>
        /// <param name="type">按钮类型</param>
        /// <returns></returns>
        public FlexiGridButton Bind(string title, FlexiGridButtonType type)
        {
            return Bind(type.ToString(), title).Type(type);
        }

        /// <summary>
        /// 添加一个按钮
        /// </summary>
        /// <param name="name">按钮名称</param>
        /// <param name="title">按钮显示标题</param>
        /// <returns></returns>
        public FlexiGridButton Bind(string name, string title)
        {
            var button = new FlexiGridButton(name, title);
            button.ToolTip(title);
            this._buttons.Add(button);
            return button;
        }

        #endregion

        #region Implementation of IEnumerable

        public IEnumerator<FlexiGridButton> GetEnumerator()
        {
            return this._buttons.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion

        #region Implementation of ICollection<FlexiGridButton>

        public void Add(FlexiGridButton item)
        {
            this._buttons.Add(item);
        }

        public void Clear()
        {
            this._buttons.Clear();
        }

        public bool Contains(FlexiGridButton item)
        {
            return this._buttons.Contains(item);
        }

        public void CopyTo(FlexiGridButton[] array, int arrayIndex)
        {
            this._buttons.CopyTo(array, arrayIndex);
        }

        public bool Remove(FlexiGridButton item)
        {
            return this._buttons.Remove(item);
        }

        public int Count
        {
            get { return this._buttons.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        #endregion
    }
}
